﻿namespace USElections.Map
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Media;
    using System.Xml.Linq;
    using Microsoft.Phone.Controls.Maps;
    using USElections.Converters;
    using USElections.Model;

    /// <summary>
    /// Renderer class.
    /// </summary>
    public class Renderer
    {
        /// <summary>
        /// Draw the states of the US on the map.
        /// </summary>
        /// <param name="map">The map to draw the states on.</param>
        /// <param name="states">The underlying states data.</param>
        public static void DrawStates(Microsoft.Phone.Controls.Maps.Map map, USElections.Model.JSON.States states)
        {
            if (states == null || map == null)
            {
                return;
            }

            BackgroundWorker bgw = new BackgroundWorker();
            bgw.DoWork += (sender, e) =>
            {
                PartyToColorConverter ptcc = new PartyToColorConverter();

                XDocument doc = XDocument.Load("Data/states.xml");

                foreach (XElement stateElement in doc.Elements("states").Elements("state"))
                {
                    ////string name = state.Attribute("name").Value;
                    string code = stateElement.Attribute("code").Value;

                    LocationCollection coll = new LocationCollection();

                    foreach (XElement polygon in stateElement.Elements("point"))
                    {
                        double lat = Double.Parse(polygon.Attribute("lat").Value);
                        double lng = Double.Parse(polygon.Attribute("lng").Value);

                        coll.Add(new System.Device.Location.GeoCoordinate(lat, lng));
                    }

                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        Brush color = new SolidColorBrush(Colors.Gray) { Opacity = 0.3 };

                        USElections.Model.JSON.State state = states[code];

                        if (state != null)
                        {
                            Candidate.Parties candidateParty = state.GetLeadingParty();

                            if (candidateParty != Candidate.Parties.None)
                            {
                                color = (Brush)ptcc.Convert(candidateParty, typeof(Brush), null, null);
                                color.Opacity = 0.75;
                            }
                        }

                        MapPolygon pl = new MapPolygon();
                        pl.Locations = coll;
                        pl.Stroke = new SolidColorBrush(Colors.Orange);
                        pl.StrokeThickness = 1;

                        pl.Fill = color;

                        map.Children.Add(pl);
                    });
                }
            };

            bgw.RunWorkerAsync();
        }
    }
}
